package test.pfsimulator

import actors.{TIMEOUT, Actor}

/**
 * Created with IntelliJ IDEA.
 * User: codefish
 * Date: 11/5/12
 * Time: 12:02 PM
 * To change this template use File | Settings | File Templates.
 */
case class Interrupt(msg: String)
object TestTimeoutActor extends Actor{
    def act{
        var t = System.currentTimeMillis()
        var startTime = t
        val s = "tick"
        val timeUnit = 1000.toLong
        var waitTime = timeUnit
        loop {
            reactWithin(waitTime){
                case TIMEOUT =>
                    println((System.currentTimeMillis()-startTime)/1000.toFloat + s)
                    t = System.currentTimeMillis()
                    waitTime = timeUnit
                case Interrupt(msg) =>
                    println((System.currentTimeMillis()-startTime)/1000.toFloat + msg)
                    waitTime = timeUnit-(System.currentTimeMillis()-t)
            }
        }
    }
    def main(args: Array[String]) {
        TestTimeoutActor.start
        Thread.sleep(2500)
        TestTimeoutActor ! Interrupt("interrupt")
        Thread.sleep(2500)
        TestTimeoutActor ! Interrupt("interrupt")
        Thread.sleep(2500)
        TestTimeoutActor ! Interrupt("interrupt")
        Thread.sleep(2500)
        TestTimeoutActor ! Interrupt("interrupt")
    }
}
